package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.WareHouseQualityMapper;
import cn.tedu.erp.project.pojo.dto.WareHouseQualityAddNewDTO;
import cn.tedu.erp.project.pojo.dto.WareHouseQualityUpdateDTO;
import cn.tedu.erp.project.pojo.entity.WareHouseQuality;
import cn.tedu.erp.project.pojo.vo.WareHouseQualityListVO;
import cn.tedu.erp.project.pojo.vo.WareHouseQualityStandardVO;
import cn.tedu.erp.project.service.IWareHouseQualityService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class WareHouseQualityServiceImpl implements IWareHouseQualityService {
    
    @Autowired
    private WareHouseQualityMapper mapper;
    
    @Override
    public void addNew(WareHouseQualityAddNewDTO wareHouseQualityAddNewDTO) {
        log.debug("开始处理【添加质量检查结果】的业务，参数：{}", wareHouseQualityAddNewDTO);
        // 调用参数对象的getName()得到尝试添加的质量检查结果的名称
        String name = wareHouseQualityAddNewDTO.getName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = mapper.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);

        // 创建WareHouseQuality对象
        WareHouseQuality wareHouseQuality = new WareHouseQuality();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到WareHouseQuality对象中
        BeanUtils.copyProperties(wareHouseQualityAddNewDTO, wareHouseQuality);
        // 调用Mapper对象的insert()执行插入质量检查结果数据
        log.debug("即将执行插入数据，参数：{}", wareHouseQuality);
        mapper.insert(wareHouseQuality);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除质量检查结果】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        WareHouseQualityStandardVO wareHouseQuality = mapper.getStandardById(id);
        log.debug("根据ID={}检查质量检查结果数据是否存在，查询结果：{}", id, wareHouseQuality);
        // 判断查询结果是否为null
        if (wareHouseQuality == null) {
            // 是：数据不存在，抛出异常
            String message = "删除质量检查结果失败，尝试删除的质量检查结果数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        mapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, WareHouseQualityUpdateDTO wareHouseQualityUpdateDTO) {
        log.debug("开始处理【修改质量检查结果详情】的业务，ID：{}，新数据：{}", id, wareHouseQualityUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        WareHouseQualityStandardVO queryResult = mapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改质量检查结果详情失败，尝试修改的质量检查结果数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 创建WareHouseQuality对象
        WareHouseQuality wareHouseQuality = new WareHouseQuality();
        // 复制属性，设置ID
        BeanUtils.copyProperties(wareHouseQualityUpdateDTO, wareHouseQuality);
        wareHouseQuality.setId(id);
        // 调用Mapper对象的update()方法执行修改
        mapper.update(wareHouseQuality);
    }

    @Override
    public WareHouseQualityStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询质量检查结果详情】的业务，参数：{}", id);
        // WareHouseQualityStandardVO queryResult = mapper.getStandardById(id);
        WareHouseQualityStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询质量检查结果详情失败，质量检查结果数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<WareHouseQualityListVO> list() {
        log.debug("开始处理【查询质量检查结果列表】的业务，无参数");
        // List<WareHouseQualityListItemVO> list = mapper.list();
        List<WareHouseQualityListVO> list = mapper.list();
        return list;
    }

    @Override
    public void rebuildCache() {

    }
}
